package com.kitty.common.utils;

import com.kitty.cross.core.callback.CallBackService;
import com.kitty.game.activity.service.ActivityService;
import com.kitty.game.artifact.ArtifactService;
import com.kitty.game.boss.service.NewBossService;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.drop.service.DropService;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fight.service.BroadcastService;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.fight.service.FightMessageService;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.friend.service.FriendService;
import com.kitty.game.guard.service.GuardService;
import com.kitty.game.hunqi.HunQiService;
import com.kitty.game.identity.IdentityService;
import com.kitty.game.mail.service.MailService;
import com.kitty.game.map.service.MapService;
import com.kitty.game.mirror.service.FightMirrorService;
import com.kitty.game.npc.service.NewNpcService;
import com.kitty.game.party.service.PartyService;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.player.PlayerService;
import com.kitty.game.rank.service.RankService;
import com.kitty.game.role.service.AccountService;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.skill.service.SkillService;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.team.service.TeamService;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.kitty.web.common.StringUtils;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.kitty.game.luoshu.service.LuoshuService;//洛书
import com.kitty.game.fuling.service.FulingService;//附灵

@Component
public class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {

    private static ApplicationContext applicationContext;


    private static SpringUtils self;
    /** Spring应用上下文环境 */
    private static ConfigurableListableBeanFactory beanFactory;

    @PostConstruct
    private void init() {
        self = this;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.applicationContext = applicationContext;
    }


    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        SpringUtils.beanFactory = configurableListableBeanFactory;
    }
    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return beanFactory.getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return beanFactory.getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return beanFactory.getBean(name, clazz);
    }



    public static <T> Map<String, T> getBeansOfType(Class<T> var1) {
        return beanFactory.getBeansOfType(var1);
    }

    /**
     * 获取aop代理对象
     *
     * @param invoker
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getAopProxy(T invoker)
    {
        return (T) AopContext.currentProxy();
    }

    /**
     * 获取当前的环境配置，无配置返回null
     *
     * @return 当前的环境配置
     */
    public static String[] getActiveProfiles()
    {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    /**
     * 获取当前的环境配置，当有多个环境配置时，只获取第一个
     *
     * @return 当前的环境配置
     */
    public static String getActiveProfile()
    {
        final String[] activeProfiles = getActiveProfiles();
        return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
    }





    public final static RoleService getRoleService() {
        return beanFactory.getBean(RoleService.class);
    }

    
    public final static CountService getHurtService() {
    	return beanFactory.getBean(CountService.class);
    }

    
    public final static GuardService getGuardService() {
    	return beanFactory.getBean(GuardService.class);
    }
    

    
    public final static FightService getFightService() {
    	return beanFactory.getBean(FightService.class);
    }

    
    public final static EquipService getEquipService() {
    	return beanFactory.getBean(EquipService.class);
    }




    public final static PlayerService getPlayerService() {
        return beanFactory.getBean(PlayerService.class);
    }




    public final static PetService getPetService() {
        return beanFactory.getBean(PetService.class);
    }




    public final static SkillService getSkillService() {
        return beanFactory.getBean(SkillService.class);
    }


    public final static BroadcastService getBroadcastService() {
        return beanFactory.getBean(BroadcastService.class);
    }



    public final static MapService getMapService() {
        return beanFactory.getBean(MapService.class);
    }



    public final static TeamService getTeamService() {return beanFactory.getBean(TeamService.class); }



    public final static ChatService getChatService() {return beanFactory.getBean(ChatService.class); }



    public final static ActivityService getActivityService() {return beanFactory.getBean(ActivityService.class); }



    public final static NewNpcService getNpcService() {return beanFactory.getBean(NewNpcService.class); }



    public final static NewTaskService getTaskService() {return beanFactory.getBean(NewTaskService.class); }


    public final static FightMirrorService getMirrorService() {return beanFactory.getBean(FightMirrorService.class); }


    public final static IdentityService getIdentityService() {
        return beanFactory.getBean(IdentityService.class);
    }



    public final static FightMessageService getFightMessageService() {
        return beanFactory.getBean(FightMessageService.class);
    }



    public final static DropService getDropService() {return beanFactory.getBean(DropService.class); }



    public final static MailService getMailService() {return beanFactory.getBean(MailService.class); }

    public final static NewBossService getBossService() {return beanFactory.getBean(NewBossService.class); }



    public final static AccountService getAccountService() {
        return beanFactory.getBean(AccountService.class);
    }



    public final static RankService getRankService() {
        return beanFactory.getBean(RankService.class);
    }



    public final static ArtifactService getArtifactService() {
        return beanFactory.getBean(ArtifactService.class);
    }



    public final static PartyService getPartyService() {
        return beanFactory.getBean(PartyService.class);
    }


    public final static FriendService getFriendService() {
        return beanFactory.getBean(FriendService.class);
    }



    public final static CallBackService getCallBackService() {
        return beanFactory.getBean(CallBackService.class);
    }



    public static final FulingService getFulingService() {
        return beanFactory.getBean(FulingService.class);
    }
    //洛书


    public static final LuoshuService getLuoshuService() {
        return beanFactory.getBean(LuoshuService.class);
    }



    public final static HunQiService getHunQiService() {
        return beanFactory.getBean(HunQiService.class);
    }


}
